//
//  YZMSegmentViewFixed.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/24.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMSegmentViewFixed.h"
#import "YZMAppStyle.h"

#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

static const float segment_view_margin_for_red_line = 20;

@interface YZMSegmentViewFixed()<UIScrollViewDelegate>

@end

@implementation YZMSegmentViewFixed


#pragma mark  -- Public Method

/*设置标题列表*/
- (void)setTitleArray:(NSArray *)titleArray{
    
    [_titleView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_buttonArray removeAllObjects];
    
    _titleArray = titleArray;

    if (!titleArray || titleArray.count == 0) {
        
        return;
    }
    
    NSUInteger titleCount = [titleArray count];
    UIButton *lastObject = nil;
    
    for (NSString *title in titleArray) {
        
        UIButton *button = [UIButton new];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[YZMAppStyle colorForMainTextColor] forState:UIControlStateNormal];
        [button setTitleColor:[YZMAppStyle colorForIconColor] forState:UIControlStateSelected];
        [button.titleLabel setFont:[YZMAppStyle mediumSizeFont]];
        button.tag = [titleArray indexOfObject:title];
        [button addTarget:self action:@selector(clickForSwitchView:) forControlEvents:UIControlEventTouchUpInside];
        [_titleView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.height.equalTo(_titleView);
            maker.width.equalTo(_titleView).dividedBy(titleCount);
            
            if (lastObject) {
                
                maker.left.mas_equalTo(lastObject.mas_right);
                
            }else{
                maker.left.mas_equalTo(_titleView);
            }
            
        }];
        
        
        if (button.tag == 0) {
            
            button.selected = YES;
            
            _redLineView = [UIView new];
            _redLineView.backgroundColor = [YZMAppStyle colorForIconColor];
            [self addSubview:_redLineView];
            [_redLineView mas_makeConstraints:^(MASConstraintMaker *maker){
                
                maker.width.mas_equalTo(button.mas_width).offset(-segment_view_margin_for_red_line);
                maker.left.mas_equalTo(segment_view_margin_for_red_line/2);
                maker.centerY.mas_equalTo(button.mas_bottom);
                maker.height.mas_equalTo(3);
            }];
        }
        
        [_buttonArray addObject:button];
        lastObject = button;
    }
}

/*设置滑动的页面视图列表*/
- (void)setViewArray:(NSArray *)viewArray{
    
    if (!_titleArray || !viewArray || _titleArray.count != viewArray.count) {
        
#ifdef DEBUG
        NSAssert(0, @"视图个数与 标题个数不符。");
#endif
        return;
    }
    
    _viewArray = viewArray;
    
    UIView *lastView = nil;
    
    for (UIView *view in _viewArray) {
        
        [_containerView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.bottom.equalTo(_containerView);
            make.width.equalTo(_contentSV);
            if (lastView) {
                
                make.left.equalTo(lastView.mas_right);
            }else{
                
                make.left.equalTo(_containerView);
            }
        }];
        
        lastView = view;
    }
    
    [_containerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastView);
    }];
}

- (void)addConstraints{
    
    WS(ws)
    
    [_titleView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.left.width.equalTo(ws);
        maker.height.mas_equalTo(44);
        
    }];
    
    UIView *line = [YZMAppStyle splitLine];
    [self addSubview:line];
    [self insertSubview:line aboveSubview:_titleView];
    [line mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.left.width.equalTo(_titleView);
        make.bottom.equalTo(_titleView.mas_bottom).offset(-1);
    }];
    
    [_contentSV mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.mas_equalTo(_titleView.mas_bottom);
        maker.left.right.bottom.equalTo(ws);
    }];
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.height.equalTo(_contentSV);
    }];
}

@end
